﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JZNJ.RadioMonitorServer.Core
{
   public class RadioPainter
    {
       private static Brush brush_backGround = Brushes.Black;
       private static Pen pen_Grid = Pens.WhiteSmoke;
       private static Pen pen_Wave = Pens.Aqua;
       private static Brush brush_Text = Brushes.Red;
       private static Pen pen_coordinateAxis = Pens.White;
       private static int margin = 60;
       private static int radius = 10;
       /// <summary>
       /// 纵坐标刻度个数
       /// </summary>
       private static int ordinateScaleCount=8;
       /// <summary>
       /// 横坐标刻度个数
       /// </summary>
       private static int abscissaScalCount=10;
       private Rectangle rectangleClient;
       private Rectangle rectanleDrawWave;
       /// <summary>
       /// 要绘制的数组
       /// </summary>
       private List<DeviceInfo> values;
       //纵坐标最小值
       private double minOrdinates;
       //纵坐标最大值
       private double maxOrdinates;
       private string textAbscissa = "频率";
       private string textOrdinates = "功率";
       //横坐标最大值
       private double maxAbscissa=0;
       //横坐标最小值
       private double minAbscissa=0;

       /// <summary>
       /// 绘制网格
       /// </summary>
       /// <param name="gridArea"></param>
       /// <param name="gs"></param>
       private void DrawGrid(Graphics gs)
       {
           Pen pen = new Pen(pen_Grid.Color);
           pen.DashStyle = DashStyle.DashDot;
           var tap= rectanleDrawWave.Width / abscissaScalCount;
           for(int i=0;i<tap;i++)
           {
               gs.DrawLine(pen, rectanleDrawWave.Left + i * tap, rectanleDrawWave.Bottom, rectanleDrawWave.Left + i * tap, rectanleDrawWave.Top);
           }
           tap = rectanleDrawWave.Height / ordinateScaleCount;
           for (int i = 0; i < tap; i++)
           {
               gs.DrawLine(pen, rectanleDrawWave.Left , rectanleDrawWave.Top+i*tap, rectanleDrawWave.Right , rectanleDrawWave.Top+i*tap);
           }
           pen.Dispose();
       }

       /// <summary>
       /// 绘制坐标
       /// </summary>
       /// <param name="gs"></param>
       private void DrawAxis(Graphics gs)
       {
           Pen pen = new Pen(pen_coordinateAxis.Color,2.0f);
           pen.EndCap = LineCap.ArrowAnchor;
           gs.DrawLine(pen, rectanleDrawWave.Left - 20, rectanleDrawWave.Bottom, rectanleDrawWave.Right + 40, rectanleDrawWave.Bottom);
           gs.DrawLine(pen, rectanleDrawWave.Left, rectanleDrawWave.Bottom + 20, rectanleDrawWave.Left, rectanleDrawWave.Top - 40);
           pen.Dispose();
       }

       /// <summary>
       ///绘制值 
       /// </summary>
       /// <param name="gs"></param>
       private void DrawVules(Graphics gs)
       {
          var temp= (this.maxAbscissa - this.minAbscissa)/(this.rectanleDrawWave.Width);
          double arg = 0.125;
          var temp2 = (this.maxOrdinates*(1+arg) - this.minOrdinates*(1-arg))/this.rectanleDrawWave.Height;
          PointF[] points = new PointF[this.values.Count];
          for (int i = 0; i < points.Length; i++)
          {
              float x = (float)((this.values[i].Fre - minAbscissa) / temp+this.rectanleDrawWave.Right);
              float y = (float)(this.rectanleDrawWave.Bottom - (this.values[i].Value - this.minOrdinates * (1 - arg)) / temp2);
              points[i]= new PointF(x,y);
          }
           GraphicsPath gp = new GraphicsPath();
           gp.AddLines(points);
           gs.DrawPath(pen_Wave, gp);
           gp.Dispose();
       }

       private void Refresh(Graphics gs,Rectangle retanleClient)
       {
           gs.FillRectangle(brush_backGround, rectangleClient);
           this.rectanleDrawWave = new Rectangle(rectangleClient.X + margin, rectangleClient.Y + margin, rectangleClient.Width - 2 * margin, rectangleClient.Height - 2 * margin);
           DrawGrid(gs);
           DrawAxis(gs);
           DrawVules(gs);

       }

       public void AddValue(DeviceInfo info)
       {
           if (this.values.Count == 0)
           {
               this.maxOrdinates = info.Value;
               this.minOrdinates = info.Value;
           }
           else
           {
               if (this.maxOrdinates < info.Value)
               {
                   this.maxOrdinates = info.Value;
               }
               else if (this.minOrdinates > info.Value)
               {
                   this.minOrdinates = info.Value;
               }
           }
           this.values.Add(info);
       }
    }
}
